package com.jch.helloworld.configBeans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {

    private static void getBean0(){
        // ApplicationContext 代表IOC容器，ClassPathXmlApplicationContext 是其实现类，该类实现了从类路径下加载xml配置文件功能。
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans/DemoBeans.xml");
        DemoBeans demoBean = (DemoBeans) ctx.getBean("demoBean0"); // 使用Bean ID获取对应的Bean实例
        System.out.println(demoBean);
//        DemoBeans demoBean1 = ctx.getBean(DemoBeans.class); // 使用类型获取对应的Bean实例，该方法要求IOC容器该种类型的Bean只有一个。
//        System.out.println(demoBean1);
    }

    private static void getBean1(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans/DemoBeans.xml");
        // 通过setter方法设置指定属性值
        DemoBeans demoBean1 = (DemoBeans) ctx.getBean("demoBean1");
        System.out.println(demoBean1);
    }

    private static void getBean2(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans/DemoBeans.xml");
        // 使用构造器注入属性值：按索引匹配入参
        DemoBeans demoBean2 = (DemoBeans) ctx.getBean("demoBean2");
        System.out.println(demoBean2);
    }

    private static void getBean3(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans/DemoBeans.xml");
        // 使用构造器注入：按类型匹配入参
        DemoBeans demoBean3 = (DemoBeans) ctx.getBean("demoBean3");
        System.out.println(demoBean3);
    }

    private static void getBean4(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans/DemoBeans.xml");
        // 使用构造器注入：索引和类型混合匹配入参
        DemoBeans demoBean4 = (DemoBeans) ctx.getBean("demoBean4");
        System.out.println(demoBean4);
    }

    private static void getBean5(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans/DemoBeans.xml");
        // 使用构造器注入：按参数名称匹配入参
        DemoBeans demoBean5 = (DemoBeans) ctx.getBean("demoBean5");
        System.out.println(demoBean5);
    }

    public static void main(String[] args) {
        System.out.println("获取bean0: ");
        getBean0();
        System.out.println("获取bean1：");
        getBean1();
        System.out.println("获取bean2(使用构造器注入，按索引匹配入参)：");
        getBean2();
        System.out.println("获取bean3(使用构造器注入，按类型匹配入参)：");
        getBean3();
        System.out.println("获取bean4(使用构造器注入，索引和类型混合匹配入参)：");
        getBean4();
        System.out.println("获取bean5(使用构造器注入，按参数名称匹配入参)：");
        getBean5();
    }

}
